import {WebContentsPrintOptions} from "electron";
import {IpcChannel, IpcMainHandle} from "../ipc";

import {openPrintDemoWindow} from "../hook/printHook";

export function usePrintHandle(): Pick<
	IpcMainHandle,
	| IpcChannel.GetPrinters
	| IpcChannel.PrintHandlePrint
	| IpcChannel.OpenPrintDemoWindow
> {
	return {
		[IpcChannel.GetPrinters]: async (event) => {
			return await event.sender.getPrintersAsync();
		},
		[IpcChannel.PrintHandlePrint]: async (
			event,
			options: WebContentsPrintOptions
		) => {
			return new Promise((resolve) => {
				event.sender.print(
					options,
					(success: boolean, failureReason: string) => {
						resolve({success, failureReason});
					}
				);
			});
		},
		[IpcChannel.OpenPrintDemoWindow]: () => {
			openPrintDemoWindow();
		},
	};
}
